package 面试2025.滴滴;

public class 岛屿数量 {
    public static void main(String[] args) {
        int[][] arr = new int[][]{{1, 1, 0, 0, 0}, {1, 1, 0, 0, 0}, {0, 0, 1, 0, 0}, {0, 0, 0, 1, 1}};
        int judge = judge(arr);
        System.out.println(judge);
    }
    private static int judge(int[][] arr){
        int m = arr.length, n = arr[0].length;
        int res = 0;
        for(int i = 0;i < m;i++){
            for(int j = 0;j < n;j++){
                if(arr[i][j] == 1){
                    dfs(arr, i, j);
                    res++;
                }
            }
        }
        return res;
    }
    private static void dfs(int[][] arr, int i, int j){
        if(i < 0 || i >= arr.length || j < 0 || j >= arr[0].length){
            return;
        }
        if(arr[i][j] != 1){
            return;
        }
        arr[i][j] = 2;
        dfs(arr, i - 1, j);
        dfs(arr, i + 1, j);
        dfs(arr, i, j - 1);
        dfs(arr, i, j + 1);
    }
}
